<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 组件-上传车辆图片 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="component-modal-photograph" @click.stop>
		<uni-popup ref="popupModal" type="center" @change="onChange">
			<view class="popup-box" :style="{'--theme-color': themeColor}">
				<view class="popup-header">
					<view class="title">请上传车辆图片</view>
					<view class="subtitle">请拍摄全方位车辆照片，图片不强制上传</view>
					<image class="close" src="/static/close.png" mode="aspectFit" @click="onClose"></image>
				</view>
				<scroll-view scroll-y class="popup-content">
					<view class="content-upload flex">
						<view class="upload-image" v-if="selectImages.length > 0" v-for="(img, num) in selectImages" :key="num" @click="previewImage(num)">
							<image class="image-select" :src="img" mode="aspectFill"></image>
							<image class="image-delete" src="/static/driver/delete.png" mode="aspectFit" @click.stop="deleteImage(num)"></image>
						</view>
						<view class="upload-image" v-if="selectImages.length < 9" @click="chooseImage()">
							<view class="image-background"></view>
							<view class="image-choose">
								<view class="icon">
									<image src="/static/driver/camera.png" mode="aspectFit"></image>
								</view>
								<view class="text">上传图片</view>
							</view>
						</view>
					</view>
				</scroll-view>
				<view class="popup-btn flex flex-between-center">
					<view class="btn" @click="onConfirm(1)">立即上传</view>
					<view class="btn plain" @click="onConfirm(2)">暂不上传</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		name: "componentModalPhotograph",
		data() {
			return {
				// 弹窗类型
				modalType: null,
				// 已选择图片
				selectImages: [],
				// 已上传图片
				uploadedImages: [],
			};
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		methods: {
			// 打开模态框
			open(type, images) {
				this.modalType = type
				if (images && images.length) {
					try {
						if (typeof(images) == "string") {
							this.selectImages = this.uploadedImages = images.split(",")
						} else {
							this.selectImages = this.uploadedImages = images
						}
					} catch (error) {
						this.selectImages = this.uploadedImages = images
					}
				} else {
					this.selectImages = this.uploadedImages = []
				}
				this.$refs.popupModal.open()
			},
			// 关闭弹窗
			onClose() {
				this.$refs.popupModal.close()
			},
			// 改变事件
			onChange(e) {
				this.$emit("onChange", e.show)
			},
			// 选择图片
			chooseImage() {
				// #ifdef MP-WEIXIN
				uni.chooseMedia({
					count: 9 - this.selectImages.length,
					mediaType: ['image'],
					sourceType: ['camera'],
					sizeType: ['compressed'],
					success: (res) => {
						this.selectImages = [...this.selectImages, ...res.tempFiles.map(item => item.tempFilePath)]
					}
				})
				// #endif
				// #ifndef MP-WEIXIN
				uni.chooseImage({
					count: 9 - this.selectImages.length,
					sourceType: ['camera'],
					sizeType: ['compressed'],
					success: (res) => {
						this.selectImages = [...this.selectImages, ...res.tempFilePaths]
					}
				});
				// #endif
			},
			// 删除图片
			deleteImage(index) {
				this.$delete(this.selectImages, index)
			},
			// 预览图片
			previewImage(index) {
				uni.previewImage({
					urls: this.selectImages,
					current: index,
				})
			},
			// 确认
			onConfirm(type) {
				if (type == 1) {
					if (!this.selectImages.length) {
						uni.showToast({
							title: "请上传车辆图片",
							icon: "none"
						})
						return
					}
					uni.showLoading({
						title: "加载中",
						mask: true,
					})
					this.$util.uploadFileMultiple(this.selectImages, this.uploadedImages).then(result => {
						this.$emit("onConfirm", this.modalType, result)
					}).catch(error => {
						console.error('上传文件 ', error)
					})
				} else {
					if (this.modalType == 2) {
						uni.showLoading({
							title: "加载中",
							mask: true,
						})
						this.$emit("onConfirm", this.modalType)
					}
					this.onClose()
				}
			},
		},
	}
</script>

<style lang="scss">
	.component-modal-photograph {
		position: relative;
		z-index: 999;

		.popup-box {
			background: #FFFFFF;
			border-radius: 32rpx;
			padding: 32rpx 0;
			width: calc(100vw - 64rpx);

			.popup-header {
				padding: 0 32rpx;
				position: relative;

				.title {
					color: #242629;
					font-size: 32rpx;
					font-weight: 600;
					line-height: 48rpx;
				}

				.subtitle {
					color: #8D929C;
					font-size: 28rpx;
					font-weight: 600;
					line-height: 40rpx;
					margin-top: 16rpx;
				}

				.close {
					width: 48rpx;
					height: 48rpx;
					position: absolute;
					top: 0;
					right: 32rpx;
				}
			}

			.popup-content {
				margin-top: 24rpx;
				padding: 0 24rpx;
				max-height: 60vh;
				min-height: 500rpx;

				.content-upload {
					display: flex;
					flex-wrap: wrap;
					row-gap: 24rpx;
					column-gap: 3.5%;
					padding: 16rpx;

					.upload-image {
						position: relative;
						width: 31%;
						height: 0;
						padding-top: 31%;

						.image-select {
							position: absolute;
							top: 0;
							left: 0;
							right: 0;
							bottom: 0;
							border-radius: 10rpx;
						}

						.image-video {
							position: absolute;
							top: 0;
							left: 0;
							right: 0;
							bottom: 0;
							border-radius: 10rpx;
							background: var(--theme-color);
							padding: 56rpx;
						}

						.image-delete {
							position: absolute;
							top: -16rpx;
							right: -16rpx;
							width: 48rpx;
							height: 48rpx;
						}

						.image-choose {
							position: absolute;
							top: 20rpx;
							left: 20rpx;
							right: 20rpx;
							bottom: 20rpx;
							z-index: 6;
							display: flex;
							flex-direction: column;
							justify-content: center;
							align-items: center;
							background: #ffffff;
							border-radius: 16rpx;

							.icon {
								width: 80rpx;
								height: 80rpx;
								padding: 18rpx;
								background: var(--theme-color);
								border-radius: 50%;
							}

							.text {
								margin-top: 16rpx;
								color: var(--theme-color);
								font-size: 28rpx;
								line-height: 40rpx;
							}
						}

						.image-background {
							position: absolute;
							top: 0;
							left: 0;
							right: 0;
							bottom: 0;
							z-index: 1;
							background: var(--theme-color);
							opacity: 0.08;
							border-radius: 16rpx;
						}
					}
				}
			}

			.popup-btn {
				margin-top: 24rpx;
				padding: 0 32rpx;

				.btn {
					width: 48%;
					color: #FFF;
					text-align: center;
					font-size: 28rpx;
					font-weight: 600;
					line-height: 40rpx;
					padding: 36rpx 48rpx;
					border-radius: 32rpx;
					overflow: hidden;
					background: var(--theme-color);

					&.plain {
						background: transparent;
						color: var(--theme-color);
						position: relative;
						z-index: 1;

						&::after {
							content: "";
							display: block;
							position: absolute;
							top: 0;
							left: 0;
							right: 0;
							bottom: 0;
							background: var(--theme-color);
							opacity: .1;
							z-index: -1;
						}
					}
				}
			}
		}
	}
</style>